<template>
  <el-dialog title="分配角色" :visible="showDialog" @close="cancel">
    <!-- 多选框 -->
    <el-checkbox-group v-model="rowIds">
      <!-- 循环的选项 -->
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <!-- 下面按钮 -->
    <el-row slot="footer" type="flex" justify="center">
      <el-col :spam="6">
        <el-button size="small" type="primary" @click="btnOK">确认</el-button>
        <el-button size="small" @click="cancel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { getUserDetailBayId } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    userId: {
      // 用户的id
      type: String,
      default: null,
      required: true
    }
  },
  data() {
    return {
      list: [], // 存储当前所以的角色ID
      rowIds: [] // 负责存储当前用户拥有的id
    }
  },
  created() {
    // 获取所以角色
    this.getRoleList()
  },
  methods: {
    // 获取所以角色
    async getRoleList() {
      const { rows } = await getRoleList({ page: 1, pagesize: 20 })
      this.list = rows
    },
    // 获取用户基本信息
    async getUserDetailBayId(id) {
      const { roleIds } = await getUserDetailBayId(id)
      this.rowIds = roleIds
    },
    cancel() {
      this.$emit('update:showDialog', false) // 将用户拥有的角色  赋值给当前用户对象
      this.rowIds = [] // 重置数组
    },
    // 点击确定
    async btnOK() {
      await assignRoles({ id: this.userId, roleIds: this.rowIds })
      this.$message.success('操作成功')
      this.$emit('update:showDialog', false) // 关闭弹层
    }
  }
}
</script>

<style>

</style>
